import request from '@/config/axios'
import { baseUrl } from '@/config/env';
/**
 * 代码生成查询
 */
export function fetchGenList(query) {
  return request({
    url: baseUrl+'/gen/generator/page',
    method: 'get',
    params: query
  })
}

/**
 * 获取表的列信息
 */
export function getTableColumn(query) {
  return request({
    url: baseUrl+'/gen/generator/tableColumn',
    params: query,
    method: 'get'
  })
}

//生成代码
export function handleGenCode(table) {
  return request({
    url: baseUrl+'/gen/generator/code',
    method: 'post',
    data: table,
    responseType: 'arraybuffer'
  }).then((response) => { // 处理返回的文件流
    const blob = new Blob([response.data], {type: 'application/zip'})
    const filename = table.tableName + '.zip'
    const link = document.createElement('a')
    link.href = URL.createObjectURL(blob)
    link.download = filename
    document.body.appendChild(link)
    link.click()
    window.setTimeout(function () {
      URL.revokeObjectURL(blob)
      document.body.removeChild(link)
    }, 0)
  })
}


export function preview(table) {
  return request({
    url: baseUrl+'/gen/generator/preview',
    method: 'get',
    params: table
  })
}
